using HalconDotNet;

namespace 作业
{
    public partial class Form1 : Form
    {
        // Local iconic variables 

        HObject ho_Image = null, ho_Red = null, ho_Green = null;
        HObject ho_Blue = null, ho_Hue = null, ho_Saturation = null, ho_Intensity = null;
        HObject ho_Saturated = null, ho_HueSaturated = null, ho_CurrentFuse = null;
        HObject ho_CurrentFuseConn = null, ho_CurrentFuseFill = null;
        HObject ho_CurrentFuseSel = null;

        // Local control variables 

        HTuple hv_FuseColors = new HTuple(), hv_FuseTypes = new HTuple();
        HTuple hv_HueRanges = new HTuple(), hv_Count = new HTuple();
        HTuple hv_WH = new HTuple(), hv_Fuse = new HTuple(), hv_FuseArea = new HTuple();
        HTuple hv_Row1 = new HTuple(), hv_Column1 = new HTuple();
        HTuple hv_i = new HTuple();
        public Form1()
        {
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_Red);
            HOperatorSet.GenEmptyObj(out ho_Green);
            HOperatorSet.GenEmptyObj(out ho_Blue);
            HOperatorSet.GenEmptyObj(out ho_Hue);
            HOperatorSet.GenEmptyObj(out ho_Saturation);
            HOperatorSet.GenEmptyObj(out ho_Intensity);
            HOperatorSet.GenEmptyObj(out ho_Saturated);
            HOperatorSet.GenEmptyObj(out ho_HueSaturated);
            HOperatorSet.GenEmptyObj(out ho_CurrentFuse);
            HOperatorSet.GenEmptyObj(out ho_CurrentFuseConn);
            HOperatorSet.GenEmptyObj(out ho_CurrentFuseFill);
            HOperatorSet.GenEmptyObj(out ho_CurrentFuseSel);
            InitializeComponent();
        }

        private void but1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp";
            openFileDialog.Title = "选择图片";
            openFileDialog.ReadOnlyChecked = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string path = openFileDialog.FileName;
               
                //面向对象方式调用Halcon
                HOperatorSet.ReadImage(out ho_Image, path);
                //在控件显示汽车图片
                hSWC1.HalconWindow.DispObj(ho_Image);
                //自适应
                hSWC1.SetFullImagePart();

            }
        }

        private void but2_Click(object sender, EventArgs e)
        {
            // color_fuses.hdev: 按颜色分类保险丝
            //****
            // 步骤：设置保险丝属性和色调范围
            //****
            hv_FuseColors.Dispose();  // 释放之前的变量资源
            hv_FuseColors = new HTuple();
            hv_FuseColors[0] = "Orange";  // 保险丝颜色：橙色
            hv_FuseColors[1] = "Red";     // 保险丝颜色：红色
            hv_FuseColors[2] = "Blue";    // 保险丝颜色：蓝色
            hv_FuseColors[3] = "Yellow";  // 保险丝颜色：黄色
            hv_FuseColors[4] = "Green";   // 保险丝颜色：绿色

            hv_FuseTypes.Dispose();
            hv_FuseTypes = new HTuple();
            hv_FuseTypes[0] = 5;   // 橙色对应5安培
            hv_FuseTypes[1] = 10;  // 红色对应10安培
            hv_FuseTypes[2] = 15;  // 蓝色对应15安培
            hv_FuseTypes[3] = 20;  // 黄色对应20安培
            hv_FuseTypes[4] = 30;  // 绿色对应30安培

            // 色调范围定义（Hue Ranges）：
            // 橙色 10-30，红色 0-10，蓝色 125-162，黄色 30-64，绿色 96-128
            hv_HueRanges.Dispose();
            hv_HueRanges = new HTuple();
            hv_HueRanges[0] = 10;   // 橙色下限
            hv_HueRanges[1] = 30;   // 橙色上限
            hv_HueRanges[2] = 0;    // 红色下限
            hv_HueRanges[3] = 10;   // 红色上限
            hv_HueRanges[4] = 125;  // 蓝色下限
            hv_HueRanges[5] = 162;  // 蓝色上限
            hv_HueRanges[6] = 30;   // 黄色下限
            hv_HueRanges[7] = 64;   // 黄色上限
            hv_HueRanges[8] = 96;   // 绿色下限
            hv_HueRanges[9] = 128;  // 绿色上限

            hv_Count.Dispose();
            hv_Count = 0;  // 初始化图像计数器
           
            // 将RGB图像分解为红、绿、蓝三个通道
            ho_Red.Dispose(); ho_Green.Dispose(); ho_Blue.Dispose();
            HOperatorSet.Decompose3(ho_Image, out ho_Red, out ho_Green, out ho_Blue);

            // 将RGB色彩空间转换为HSV（Hue色调, Saturation饱和度, Value明度）
            ho_Hue.Dispose(); ho_Saturation.Dispose(); ho_Intensity.Dispose();
            HOperatorSet.TransFromRgb(ho_Red, ho_Green, ho_Blue, out ho_Hue, out ho_Saturation,
                out ho_Intensity, "hsv");

            // 筛选出高饱和度区域（饱和度60-255）
            ho_Saturated.Dispose();
            HOperatorSet.Threshold(ho_Saturation, out ho_Saturated, 60, 255);

            // 在高饱和度区域内提取色调信息（缩小处理范围）
            ho_HueSaturated.Dispose();
            HOperatorSet.ReduceDomain(ho_Hue, ho_Saturated, out ho_HueSaturated);
            for (hv_Fuse = 0; (int)hv_Fuse <= (int)((new HTuple(hv_FuseTypes.TupleLength())) - 1); hv_Fuse = (int)hv_Fuse + 1)
            {
                //****
                //step: classify specific fuse
                //****
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    ho_CurrentFuse.Dispose();
                    HOperatorSet.Threshold(ho_HueSaturated, out ho_CurrentFuse, hv_HueRanges.TupleSelect(
                        hv_Fuse * 2), hv_HueRanges.TupleSelect((hv_Fuse * 2) + 1));
                }
                ho_CurrentFuseConn.Dispose();
                HOperatorSet.Connection(ho_CurrentFuse, out ho_CurrentFuseConn);
                ho_CurrentFuseFill.Dispose();
                HOperatorSet.FillUp(ho_CurrentFuseConn, out ho_CurrentFuseFill);
                ho_CurrentFuseSel.Dispose();
                HOperatorSet.SelectShape(ho_CurrentFuseFill, out ho_CurrentFuseSel, "area",
                    "and", 6000, 20000);
                hv_FuseArea.Dispose(); hv_Row1.Dispose(); hv_Column1.Dispose();
                HOperatorSet.AreaCenter(ho_CurrentFuseSel, out hv_FuseArea, out hv_Row1,
                    out hv_Column1);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "magenta");
                }
                for (hv_i = 0; (int)hv_i <= (int)((new HTuple(hv_FuseArea.TupleLength())) - 1); hv_i = (int)hv_i + 1)
                {
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HOperatorSet.SetTposition(hSWC1.HalconWindow, hv_Row1.TupleSelect(hv_i), hv_Column1.TupleSelect(
                            hv_i));
                    }
                    using (HDevDisposeHelper dh = new HDevDisposeHelper())
                    {
                        HOperatorSet.WriteString(hSWC1.HalconWindow, (((hv_FuseColors.TupleSelect(hv_Fuse)) + " ") + (hv_FuseTypes.TupleSelect(
                            hv_Fuse))) + " Ampere");
                    }
                }
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    HOperatorSet.SetTposition(hSWC1.HalconWindow, 24 * (hv_Fuse + 1), 12);
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "slate blue");
                }
                using (HDevDisposeHelper dh = new HDevDisposeHelper())
                {
                    HOperatorSet.WriteString(hSWC1.HalconWindow, ((hv_FuseColors.TupleSelect(hv_Fuse)) + " Fuses: ") + (new HTuple(hv_FuseArea.TupleLength()
                        )));

                }

                richTextBox1.AppendText(((hv_FuseColors.TupleSelect(hv_Fuse)) + "保险丝：") + (new HTuple(hv_FuseArea.TupleLength())) + "\n");
            }
            
        }
    }
}
